/**
 * 通用列表模块
 * 
 * @Date: 2016-1-24
 * @author: Jackson wang
 * @version: 1.0
 * @since: 1.0
 */
define(["jquery", "msgServ","ajaxServ","datagridServ","sysmanMain","btnServ","bizProto"], 
	function($,msgServ,ajaxServ,datagridServ,sysmanMain,btnServ,bizProto) {

	'use strict';

	var    $dgtype,$gridtype,$dgdetail,$griddetail,$edit_window,
			_defaultConfig,_moduleConfig,_buttonConfig,_bizProto,
			$edit_window_height=700,$edit_window_width=900,
			typeQryUrl,
			_defaultConfig ={
				doc:null,
				rootId:null,				
				panelSelector:'div.well',
				subSystemCode:null,
				moduleCode:null,
				typeCode:null,//分类模块代码
				typeField:null,//分类字段名
				editWindow:null,
				btnSelector:'td.list-btn',
				initQueryParams:null,
				onDblClickRow:null,
				typeQryUrl:null,
				detailQryUrl:null
			}
		   	
    
	var	private_method = {
		init : function(_options) {
			
			_moduleConfig = sysmanMain.getCurrentModuConfig();
			if (window.console) {
				console.info(_moduleConfig.fullBizcode +' modules gets ready and been loaded.');
			}
			
					
			_defaultConfig = $.extend({},_defaultConfig,_options)			
			_bizProto = new bizProto(_defaultConfig);			
			_bizProto.gridHeight -= 15
			
			if(_defaultConfig.editWindow){
				$edit_window = $('#' + _defaultConfig.editWindow);			
			}
			
			_buttonConfig={
				"新增":private_method.add,
			    "编辑":private_method.edit,
				"删除":private_method.del					 								
			}			
			private_method.initElement(_bizProto.pageDoc)
		},		
		
		initElement: function (doc) {		
			
			
			$dgtype = $(doc).find("table.dg-type");
			
			if(_defaultConfig.typeQryUrl){
				typeQryUrl = _defaultConfig.typeQryUrl
			}else{
				typeQryUrl='/'+'/'+_defaultConfig.typeCode +
					'/query/getTreeList.action'
			}
			var typeParam = {
				height : _bizProto.gridHeight,
				url : _bizProto.rootPath + typeQryUrl				
			}
			
			if(_defaultConfig.onDblClickRow){
				typeParam.onDblClickRow = _defaultConfig.onDblClickRow
			}else{
				typeParam.onDblClickRow = private_method.getdetail
			}
			
			if(_defaultConfig.queryParam){
				typeParam.queryParams=_defaultConfig.queryParam
			}
			$gridtype=$($dgtype).datagrid(typeParam);
			//初始化操作按钮
			
			var listTd = $(doc).find(_defaultConfig.btnSelector)
			
			//加载列表按钮
			var _param = {
				bizcode:_moduleConfig.fullBizcode,
				rootPath:_bizProto.rootPath,
				container:listTd,
				callback:private_method.bindBtnEvent}
			btnServ.generateListBtn(_param);			
			
			$dgdetail = $(doc).find("table.dg-detail");			
			var _param = {
				height :_bizProto.gridHeight,
				url : _bizProto.queryListUrl,
				toolbar:$(listTd),
				title:_moduleConfig.name
			}
			
			//列表页面url配置
			if(_defaultConfig.detailQryUrl){
				_param.url = _defaultConfig.detailQryUrl
			}else{
				_param.url = _bizProto.queryListUrl
			}
				
			//初始化查询条件判断
			if(_defaultConfig.initQueryParams){
				_param.queryParams=_defaultConfig.initQueryParams
			}
			//初始化datagrid，加载数据
			$griddetail=$($dgdetail).datagrid(_param);
			
		},
		
		searchFunction:function(value,name){
			var _param = {}
	    	_param['searchName'] = name;_param['searchValue'] = value
	    	return _param;
		},
		
		
		//绑定按钮事件
		bindBtnEvent:function(){
			// 绑定按钮事件
			var btn = $(_bizProto.pageDoc).find(_defaultConfig.btnSelector +' a.function-btn')
			//判断按钮是否存在
			if(btn.length>0){
				$(btn).unbind('click');
				$(btn).bind('click', private_method.function_btn_listenner)	
			}		
		},
		// 功能按钮监听函数
		function_btn_listenner : function(e) {

			btnServ.applyButton({buttonEvt:e,buttonConfig:_buttonConfig})

		},
		 getdetail:function (rowData){
		 	var _param = {}
		 	_param[_defaultConfig.typeField]=rowData.id
		 	$dgdetail.datagrid('load',{condition:_param})			 
		},
		// 新增函数
		add : function() {
			var selectedRecord = $($gridtype).treegrid('getSelected')					
			if (!selectedRecord) {					
				msgServ.Msgslide('请选择类型');
				return
			}
			$($griddetail).datagrid('clearSelections')
			private_method.edit(false);
		},
		// 编辑,flag 为form按钮可见性标志
		edit : function(flag) {	
			if (window.console) {
				console.info( _moduleConfig.fullBizcode + ' edit on air ...');
			}
			var _param = {grid:$detailgrid,window:$edit_window,href:_bizProto.flowEidtPageUrl,width:$edit_window_width,height:$edit_window_height,flag:flag,existCheckElement:'form'}
			datagridServ.windowEdit(_param);					
		},
		
		
		// 删除
		del : function() {
			if (window.console) {
			console.info( _moduleConfig.fullBizcode + ' delete on air ...');
			}
			datagridServ.delRow({url:_bizProto.delUrl,target:$detailgrid});
		}
	}
	
	var public_method = {
		/**
		 * @param _options.doc	页面根节点
		 * @param _options.docroot	当前业务页面根节点
		 */
		init:function(_options){
			
			private_method.init(_options)
		}
	}	
	return public_method;
});